<template>
  <a-modal
    title="规则"
    width="60%"
    :visible="visible"
    :confirmLoading="loading"
    @ok="handleSubmit"
    @cancel="
      () => {
        this.visible = false
      }
    "
  >
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="10">
            <a-col :md="4" :sm="24">
              <a-form-item label="查询类别">
                <a-select allowClear v-model="queryParam.condition">
                  <a-select-option key="RuleName">规则名称</a-select-option>
                  <!-- <a-select-option key="DocumentNo">单据号</a-select-option> -->
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-form-item>
                <a-input v-model="queryParam.keyword" placeholder="关键字" />
              </a-form-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-button
                type="primary"
                @click="
                  () => {
                    this.pagination.current = 1
                    this.getDataList()
                  }
                "
                >查询</a-button
              >
              <a-button style="margin-left: 8px" @click="repossess">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <a-table
        ref="table"
        :scroll="{ y: 530 }"
        :columns="columns"
        :rowKey="(row, index) => row.Id"
        :dataSource="data"
        :pagination="pagination"
        :loading="loading"
        @change="handleTableChange"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange, type: 'radio' }"
        :bordered="true"
        size="small"
      >
        <template slot="ApprovalStatus" slot-scope="text">
          <a-tag color="green" v-if="text == 1"> 审批通过 </a-tag>
          <a-tag color="blue" v-if="!text">
            未审批
          </a-tag>
          <a-tag color="red" v-if="text == 2">
            审批驳回
          </a-tag>
        </template>
        <span slot="CreateTime" slot-scope="CreateTime">{{ timeFormat(CreateTime) }}</span>
        <span slot="AccountingStartDate" slot-scope="AccountingStartDate">{{ timeFormat(AccountingStartDate) }}</span>
        <span slot="AccountingEndDate" slot-scope="AccountingEndDate">{{ timeFormat(AccountingEndDate) }}</span>

        <span slot="action" slot-scope="text, record">
          <template>
            <a v-if="record.ApprovalStatus" @click="Check(record.Id)">查看</a>
            <!-- <a-divider v-if="record.ApprovalStatus" type="vertical" /> -->
            <a v-if="hasPerm('GMM_GiftMaterialApplyMain.Edit') && !record.ApprovalStatus" @click="handleEdit(record.Id)"
              >编辑</a
            >
            <a-divider
              v-if="
                hasPerm('GMM_GiftMaterialApplyMain.Edit') &&
                  !record.ApprovalStatus &&
                  hasPerm('GMM_GiftMaterialApplyMain.Delete')
              "
              type="vertical"
            />
            <a
              v-if="hasPerm('GMM_GiftMaterialApplyMain.Delete') && !record.ApprovalStatus"
              @click="handleDelete([record.Id])"
              >删除</a
            >
          </template>
        </span>
      </a-table>

      <!-- <edit-form ref="editForm" :disabled="disabled" :parentObj="this"></edit-form> -->
    </a-card>
  </a-modal>
</template>

<script>
import moment from 'moment'
const columns = [
  // { title: '更新时间', dataIndex: 'UpdateTime', width: '10%' },
  { title: '规则名称', dataIndex: 'RuleName' },
  {
    title: '开始效期',
    dataIndex: 'StartDate',
    align: 'center',
    customRender: value => {
      if (value) return value.substring(0, 10)
    }
  },
  {
    title: '结束效期',
    dataIndex: 'EndDate',
    align: 'center',
    customRender: value => {
      if (value) return value.substring(0, 10)
    }
  },
  // { title: '客户ID', dataIndex: 'CustomerId' },
  { title: '返料发货基础', dataIndex: 'GiftDeliveryBasics' },
  { title: '返料类型', dataIndex: 'GiftType' },
  {
    title: '状态',
    dataIndex: 'State',
    align: 'center',
    customRender: value => {
      if (!value) return '未返料'
    }
  }
  // { title: '备注', dataIndex: 'Remark' },
  // { title: '创建时间', dataIndex: 'CreateTime', scopedSlots: { customRender: 'CreateTime' } },
  //   { title: '操作', dataIndex: 'action', align: 'left', scopedSlots: { customRender: 'action' } }
]

export default {
  components: {},
  mounted() {
    this.getDataList()
  },
  data() {
    return {
      visible: false,
      data: [],
      disabled: false,
      pagination: {
        current: 1,
        pageSize: 25,
        showTotal: (total, range) => `总数:${total} 当前:${range[0]}-${range[1]}`,
        pageSizeOptions: ['25', '50', '100'], // 设置pageSize的可选值，页面可以通过下拉框进行选择
        showSizeChanger: true
      },
      filters: {},
      sorter: { field: 'Id', order: 'desc' },
      loading: false,
      columns,
      queryParam: {},
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  methods: {
    handleSubmit() {
      this.$emit('RuleName', this.selectedRows)
      this.visible = false
    },
    repossess() {
      this.queryParam = {}
      this.getDataList()
    },
    handleTableChange(pagination, filters, sorter) {
      this.pagination = { ...pagination }
      this.filters = { ...filters }

      this.getDataList()
    },
    getDataList() {
      this.selectedRowKeys = []

      this.loading = true
      this.$http
        .post('/GiftManagement/GMM_GiftMaterialRuleMain/GetDataListByConditions', {
          PageIndex: this.pagination.current,
          PageRows: this.pagination.pageSize,
          SortField: this.sorter.field || 'Id',
          SortType: this.sorter.order,
          Search: [this.queryParam],
          ...this.filters
        })
        .then(resJson => {
          this.loading = false
          this.data = resJson.Data
          if (this.data) {
            const pagination = { ...this.pagination }
            pagination.total = resJson.Total
            this.pagination = pagination
          }
        })
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    hasSelected() {
      return this.selectedRowKeys.length > 0
    },
    // handleAdd() {
    //   this.$refs.editForm.openForm(null, '新建返料申请')
    // },
    // handleEdit(id) {
    //   this.$refs.editForm.openForm(id, '编辑返料申请')
    // },
    // Check(id) {
    //   this.disabled = true
    //   this.$refs.editForm.openForm(id, '查看')
    // },
    openForm() {
      this.visible = true
      this.selectedRowKeys = []
      this.selectedRows = []
      this.pagination.current = 1
      this.queryParam = {}
      this.getDataList()
    },
    handleDelete(ids) {
      var thisObj = this
      this.$confirm({
        title: '确认删除吗?',
        onOk() {
          return new Promise((resolve, reject) => {
            thisObj.$http.post('/GiftManagement/GMM_GiftMaterialApplyMain/DeleteData', ids).then(resJson => {
              resolve()

              if (resJson.Success) {
                thisObj.$message.success('操作成功!')

                thisObj.getDataList()
              } else {
                thisObj.$message.error(resJson.Msg)
              }
            })
          })
        }
      })
    },
    timeFormat(val) {
      // 时间格式化函数
      return moment(val).format('YYYY-MM-DD')
    }
  }
}
</script>
